package com.iguo.news.util;

import java.time.Instant;
import java.util.concurrent.ThreadLocalRandom;

public class UUIDUtil {
    private static final int MAX_INT = Integer.MAX_VALUE; // 2^31-1

    /**
     * 生成int类型的UUID
     * 注意：在分布式系统中可能重复，适合单机应用
     */
    public static int generateUUID() {
        long timestamp = Instant.now().toEpochMilli();
        int random = ThreadLocalRandom.current().nextInt(0, 65536); // 0-2^16

        // 组合时间戳和随机数（取时间戳的低31位）
        return (int)((timestamp & 0x7FFFFFFF) ^ random);
    }

    /**
     * 生成正整数UUID (0到2^31-1)
     */
    public static int generatePositiveIntUUID() {
        int uuid = generateUUID();
        return uuid < 0 ? -uuid : uuid;
    }

    /**
     * 生成短数字ID (0-99999999)
     */
    public static int generateShortNumericId() {
        long timestamp = Instant.now().toEpochMilli() % 10000000; // 取时间戳后7位
        int random = ThreadLocalRandom.current().nextInt(0, 100); // 0-99
        return (int)(timestamp * 100 + random); // 组合成8位数字
    }
}